package com.app.controller;


import com.app.common.IndustryTool;
import com.app.model.CusWorkExperienceModel;
import com.app.model.CustomerModel;
import com.app.model.UserModel;
import com.app.service.CusWorkExperienceService;
import com.app.service.CustomerService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;


@Controller
@RestController
@Data
public class CusWorkExperienceController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CusWorkExperienceService cusWorkExperienceService;
    //新增修改
    @PostMapping("/groupWorkExp")
    public Result<Object> groupWorkExp(@RequestParam("companyName") String companyName,
                               @RequestParam("industry") String industry,
                               @RequestParam("workTimeStart") String workTimeStart,
                               @RequestParam("workTimeEnd") String workTimeEnd,
                               @RequestParam("postName") String postName,
                               @RequestParam("postType") Integer postType,
                               @RequestParam("duty") String duty,
                               @RequestParam("id") int id,
                               @RequestParam("workExplain") String workExplain,
                               @RequestParam("shield") int shield,
                               HttpServletRequest request
    ){
        JSONObject json = new JSONObject();
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if(user==null)
        {
            return Result.error(405,"用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        CusWorkExperienceModel cusWorkExperience = new CusWorkExperienceModel();
        cusWorkExperience.setCompanyName(companyName);
        cusWorkExperience.setIndustry(industry);
        cusWorkExperience.setWorkTimeStart(workTimeStart);
        cusWorkExperience.setWorkTimeEnd(workTimeEnd);
        cusWorkExperience.setPostName(postName);
        cusWorkExperience.setPostType(postType);
        cusWorkExperience.setDuty(duty);
        cusWorkExperience.setCustomerId(cus.getId());
        cusWorkExperience.setWorkExplain(workExplain);
        cusWorkExperience.setShield(shield);
        try {
            if(id>0)
            {
                cusWorkExperience.setId(id);
                cusWorkExperience.setUpTime(CommonHelp.getNowTimestamp());
                cusWorkExperienceService.update(cusWorkExperience);
            }else {
                cusWorkExperience.setCrtTime(CommonHelp.getNowTimestamp());
                cusWorkExperienceService.insert(cusWorkExperience);
            }
        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success(null);

    }
    //删除
    @PostMapping("/delWorkExp")
    public Result<Object> delWorkExp(int id){
        try {
            if(id>0)
            {
                cusWorkExperienceService.delete(id);
            }else {
                return Result.error(500,"ID不存在");
            }
        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success(null);
    }
    //根据用户ID查询工作经验
    @GetMapping("/getWorkExpList")
    public Object getWorkExpList(HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        String id = request.getParameter("id");

        CustomerModel cus = null;
        if (id != null && !"0".equals(id) && !"undefined".equals(id)) {
            cus = customerService.getCustomerById(Integer.valueOf(id));
        } else {
            if (token == null) {
                return Result.error(405,"token不存在");
            }
            String mobile = TokenUtils.verify(token);
            UserModel user = userService.getUserByMobile(mobile);
            if (user == null) {
                return Result.error(405,"用户不存在");
            }
            cus = customerService.getCustomerById(user.getCId());
        }
        List<CusWorkExperienceModel> workExpList = cusWorkExperienceService.getWorkExpList(cus.getId());
        if(workExpList!=null)
        {
            for (int i = 0; i < workExpList.size(); i++) {
                workExpList.get(i).setIndustryText(IndustryTool.myMap.get(workExpList.get(i).getIndustry()));
            }
        }

        return Result.success(workExpList);

    }

    //根据ID查询工作经验
    @GetMapping("/getWorkExp")
    public Object getWorkExp(HttpServletRequest request) throws ParseException {
        return Result.success(cusWorkExperienceService.getWorkExp(request));

    }
}
